<template>
  <div class="content">
    <div class="title">
      <div />
      <p>运行图表</p>
    </div>

    <div id="chart1" class="chart1" />
    <div id="chart2" class="chart2" />
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'Chart',
  props: ['chat'],
  data: () => {
    return {
      cData: []
    }
  },
  watch: {
    // 正确给 cData 赋值的 方法
    chat: function(newVal, oldVal) {
      this.cData = newVal // newVal即是chartData
      newVal && this.getEchart() // newVal存在的话执行函数
      newVal && this.getEchart1() // newVal存在的话执行函数
    }
  },
  mounted() {
    // this.getEchart()
    // this.getEchart1()
  },
  methods: {
    getEchart() {
      const chartDom = document.getElementById('chart1')
      const myChart = echarts.init(chartDom)
      let option

      option = {
        backgroundColor: '#fff',
        grid: {
          left: '0',
          right: '0',
          top: '40',
          bottom: '0',
          containLabel: true
        },
        tooltip: {
          show: true,
          trigger: 'axis'
        },
        legend: {
          show: true,
          // x: '80%',
          right: 30,
          icon: 'stack',
          itemWidth: 15,
          itemHeight: 10,
          itemGap: 20,
          textStyle: {
            color: '#646464'
          },
          data: ['昨日负荷kw', '今日负荷kw']
        },
        xAxis: [{
          type: 'category',
          boundaryGap: 1,
          axisLine: {
            show: false
          },
          axisLabel: {
            color: '#A1A7B3'
          },
          splitLine: {
            show: false
          },
          axisTick: {
            show: false
          },

          // data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
          data: this.cData[0].name
        }],
        yAxis: [{
          type: 'value',
          name: '负荷（kw）',
          padding: 5,
          // max: 1000,
          splitLine: {
            show: true,
            lineStyle: {
              color: 'rgba(176, 176, 176, .2)',
              type: 'dashed'
            }
          },
          axisLine: {
            show: false
          },
          axisLabel: {
            show: true,
            margin: 10,
            textStyle: {
              color: '#A1A7B3'

            }
          },
          axisTick: {
            show: false
          }
        }

        ],
        series: [{
          name: '今日负荷kw',
          type: 'line',
          smooth: true,
          stack: '总量',
          symbolSize: 0,
          itemStyle: {
            normal: {
              color: '#41CBA6',
              lineStyle: {
                color: '#41CBA6',
                width: 2
              }
            }
          },
          areaStyle: {
            normal: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: 'rgba(65, 203, 166, 0.6)'
                },
                {
                  offset: 1,
                  color: 'rgba(65, 203, 166, 0.2)'
                }
              ], false)
            }
          },
          // data: [120, 132, 101, 134, 90, 230, 210, 182, 191, 234, 290, 330],
          data: this.cData[0].todayData
        },
        {
          name: '昨日负荷kw',
          type: 'line',
          smooth: true,
          stack: '总量',
          symbolSize: 0,
          itemStyle: {
            normal: {
              color: '#DE625A',
              lineStyle: {
                color: '#DE625A',
                width: 2
              }
            }
          },
          areaStyle: {
            normal: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: 'rgba(222, 98, 90, 0.6)'
                },
                {
                  offset: 1,
                  color: 'rgba(222, 98, 90, 0.2)'
                }
              ], false)
            }
          },
          // data: [220, 182, 191, 234, 290, 330, 310, 201, 154, 190, 330, 410],
          data: this.cData[0].yesterdayData

        }

        ]
      }

      option && myChart.setOption(option)
      window.addEventListener('resize', () => {
        myChart.resize()
      })
    },
    getEchart1() {
      const chartDom = document.getElementById('chart2')
      const myChart = echarts.init(chartDom)
      let option

      option = {
        backgroundColor: '#fff',
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          left: '10',
          right: '0',
          bottom: '0',
          top: '40',
          containLabel: true
        },
        legend: {
          data: ['昨日电量kwh', '今日电量kwh'],
          right: 30,
          top: 12,
          textStyle: {
            color: '#B0B0B0'
          },
          itemWidth: 12,
          itemHeight: 10
        },
        xAxis: {
          type: 'category',
          // data: ['2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019'],
          data: this.cData[0].name,
          axisLine: {
            lineStyle: {
              color: '#B0B0B0'

            }
          },
          axisLabel: {
            textStyle: {
              fontFamily: 'Microsoft YaHei'
            }
          }
        },

        yAxis: {
          type: 'value',
          name: '电量（kwh）',
          padding: 5,
          axisLine: {
            show: false,
            lineStyle: {
              color: '#B0B0B0'
            }
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: 'rgba(176, 176, 176, .2)',
              type: 'solid'
            }
          }
        },
        series: [

          {
            name: '昨日电量kwh',
            type: 'bar',
            barWidth: '15%',
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                  offset: 0,
                  color: '#F7CB6B'
                }, {
                  offset: 1,
                  color: '#FBA980'
                }]),
                barBorderRadius: 11
              }

            },
            // data: [400, 500, 500, 500, 500, 400, 400, 500, 500]
            data: this.cData[0].yesterdayData

          }, {
            name: '今日电量kwh',
            type: 'bar',
            barWidth: '15%',
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                  offset: 0,
                  color: '#4CBAFF'
                }, {
                  offset: 1,
                  color: '#3E7AFF'
                }]),
                barBorderRadius: 12
              }
            },
            // data: [400, 400, 300, 300, 300, 400, 400, 400, 300]
            data: this.cData[0].todayData
          }]

      }

      option && myChart.setOption(option)
      window.addEventListener('resize', () => {
        myChart.resize()
      })
    }
  }
}
</script>

<style lang='scss' scoped>
.content {
  width: 100%;
  height: 100%;
  background: #FFFFFF;
  box-shadow: 0px 0px 20px 0px rgba(1, 43, 82, 0.1);
  border-radius: 8px;
  position: relative;
  box-sizing: border-box;
  padding: 50px 60px 0 30px;

  .chart1, .chart2 {
    width: 100%;
    height: 200px;
  }

  .title {
    width: 90px;
    position: absolute;
    left: 0;
    top: 6px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    z-index: 2;

    div {
      width: 6px;
      height: 30px;
      background: #007CF0;
      border-radius: 2px;
    }

    p {
      font-size: 17px;
      font-family: Source Han Sans CN;
      font-weight: 600;
      color: #222222;
    }
  }
}
</style>
